You can write your own functions to make your code modular and reusable. Suppose you are working on a project where you need to calculate the distance between two points, namely,
% Calculating distance of p1 and p2.
p1 = [x1, y1]
p2 = [x2, y2]
d = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2)
The above code is fine. However, if you need to compute the distance repeatedly, it becomes inconvenient and error prone. To adopt a good programming style, you should define a function named, say, distance, which is called wherever you need to calculate a distance. To define the function, you create a new .m file named distance.m and put the following code in the file:
function d = distance(p1, p2)
d = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2)
end
When you need to compute a distance, you simply call:
d = distance(p1, p2)
The code above is more modular. If you later use another mathematical expression for the distance, you only need to modify distance.m instead of making changes in multiple places of the project.
To define a function, you need to create an .m file:
distance, the file should be named distance.m.) A function definition has the following syntax:
function [out_1, ..., out_n] = fun(in_1, ..., in_m)
statement_1
statement_2
...
statment_k
end
function & end Keywordsfunction and ends with the keyword end.end is omitted, it is assumed that the whole file contains just one function.function, except whitespaces, newlines, and comments. Otherwise, the function is considered as a local function.function and end.out_1, ..., out_n are output arguments.out = fun(in_1, ..., in_m) instead.fun(in_1, ..., in_m) or [] = fun(in_1, ..., in_m).in_1, ..., in_m are the input arguments.
If it has no input arguments, you can use either one of these:
[out_1, ..., out_n] = fun()[out_1, ..., out_n] = funTo call a function, you need to make sure that the .m file defining the function is in the work directory. To facilitate our discussion, we consider a function with the following declaration:
function [out1, out2, out3] = fun(in1, in2)
%
% Some statements
%
end
You call this function by calling fun(in1, in2).
If the function definition has
[out1, out2 ,out3] = fun(in1, in2) returns all outputs.[out1, out2] = fun(in1, in2) returns the first 2 outputs.[out1] = fun(in1, in2) returns the first output.out1 = fun(in1, in2) returns the first output.fun(in1, in2) returns the first output.Each function has its own workspace. Variables defined in other workspaces are not accessible from within the function body. Similarly, variables defined in a function body are not accessible from the outside of the function. Once the execution of a function has finished, all variables in the function's workspace are cleared immediately.
A function handle is created using the @ operator. A function handle is stored in a workspace as a variable, and can be passed to another function as an input argument.
Suppose that we define the following function, which computes the absolute value of an input array:
function out1 = my_abs(in1)
out1 = abs(in1);
end
Then, we create the function handle fh of the function my_abs:
fh = @my_abs
fh = (main function)
@my_abs
Then, we can call a1 = fh(-2) to get the absolute value of -2:
a1 =
2.0000
A function handle can be passed to another function as an input argument. For example, we create the anonymous function apply = @(operation, array) operation(array), which applies a given operation to an array. In the following, r is a r.
apply = @(operation, array) operation(array)
% Random array with some negative values.
r = rand(3) - 0.5;
% Function handle of my_abs
fh = @my_abs
% Compute the absolute values of elements in r.
x = apply(fh, r)
apply = (anonymous function)
@(operation, array)operation(array)
fh = (main function)
@my_abs
x = 1e-1 ×
0.4921 0.6313 1.0245
1.0711 4.7865 1.7607
3.2648 1.8482 4.9839